package db_main

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
)

func GetLotteryAwardsByIds(ids []int) ([]models_main.LotteryAward, error) {
	fName := "GetLotteryAwardsByIds"
	var o []models_main.LotteryAward
	err := mysql.GetRTable(&models_main.LotteryAward{}).
		Where("is_delete = 0").
		Find(&o, ids).Error
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, nil
}

func GetLotteryAwardsById(id int) (*models_main.LotteryAward, error) {
	fName := "GetLotteryAwardsById"
	o := &models_main.LotteryAward{}
	err := mysql.GetRTable(&models_main.LotteryAward{}).
		Where("is_delete = 0").
		First(&o, id).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
		return nil, err
	}
	return o, nil
}

func UpdateLotteryAward(columns []string, award *models_main.LotteryAward) error {
	fName := "UpdateLotteryAward"
	err := mysql.GetXTable(models_main.LotteryAward{}).
		Where("id = ? and is_delete = 0", award.Id).
		Select(columns).
		Updates(award).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}
